<?php
/**
 * Created by PhpStorm.
 * User: lovemybud
 * Date: 15/3/15
 * Time: 23:18
 */

namespace Data;


class DataAbstract {

    static protected $_data_path        =   '';
    static protected $_instance         =   NULL;
    static protected $_initialized      =   [];
    static protected $_DATA_TYPE        =   NULL;
    static protected $_SUFFIX           =   NULL;

    static public function get ($_name, $_config = []) {
        if ($_name != NULL) self::initialize($_config, $_name);

        return self::$_instance->get($_name);
    }

    static public function set ($_name, $_data, $_config = []) {
        if ($_name != NULL) self::initialize($_config, $_name);

        return self::$_instance->set($_name, static::_encode_data($_data));
    }

    public function __get($_name) {
        return self::get($_name);
    }

    public function __set($_name, $_data) {
        return self::set($_name, $_data);
    }

    static public function initialize($_config = [], $_name = '') {

        if (isset($_config['data_path'])) {
            self::$_data_path = $_config['data_path'];
        }

        if (self::$_instance === NULL)
            self::$_instance            =   new \Yac(self::$_DATA_TYPE);

        if (empty($_name)) return TRUE;

        if (isset(self::$_initialized[$_name])) return TRUE;

        self::$_instance->set(
                                $_name,
                                static::_decode_data(
                                                    self::get_data_file($_name)
                                                )
                            );

        return TRUE;
    }

    static protected function get_data_file($_name) {
        return file_get_contents(self::$_data_path . DIRECTORY_SEPARATOR . static::$_DATA_TYPE . DIRECTORY_SEPARATOR . $_name . static::$_SUFFIX);
    }

    static protected function _decode_data($_data) {
        return $_data;
    }

    static protected function _encode_data($_data) {
        return $_data;
    }
}